<?php
// +----------------------------------------------------------------------
// | ZGUAN [ 致力于帮助更多的中小企业物联网转型 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.zguan.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ZGUAN 并不是自由软件，未经许可不能去掉ZGUAN 相关版权
// +----------------------------------------------------------------------
// | Author: ZGUAN Team <admin@zguan.cn>  0769-82282996  15899929162
// +----------------------------------------------------------------------
namespace app\common\repositories\room;

use think\facade\Db;
use app\common\dao\room\RoomOwnerDao;
use app\common\repositories\BaseRepository;
use app\common\repositories\users\UsersRepository;
use app\common\repositories\property\users\PropertyUsersLogRepository;

/**
 * Class RoomOwnerRepository
 *
 * @mixin RoomOwnerDao
 */
class RoomOwnerRepository extends BaseRepository
{

    public function getOwnerTypeData(int $id = null)
    {
        $data = [
            1 => '业主',
            2 => '亲属',
            3 => '租户',
        ];
        if ($id) {
            return $data[$id] ?? '';
        }
        return $data;
    }

    public function getAuditStatusData(int $id = null)
    {
        $data = [
            1 => '审核中',
            3 => '审核失败',
            9 => '审核成功',
        ];
        if ($id) {
            return $data[$id] ?? '';
        }
        return $data;
    }


    public function __construct(RoomOwnerDao $dao)
    {
        $this->dao = $dao;
    }

    public function getPropertyList(array $where, $page, $limit)
    {
        $query = $this->dao->search($where);
        $count = $query->count();
        $list = $query->page($page, $limit)
            ->with([
                'userInfo' => function ($query) {
                    $query->field('id,username,mobile');
                    $query->bind(['username' => 'username', 'mobile' => 'mobile']);
                },
                'roomInfo' => function ($query) {
                    $query->append(['full_room_name']);
                    $query->bind(['full_room_name' => 'full_room_name']);
                }
            ])
            ->order('id', 'desc')
            ->select();
        return compact('count', 'list');
    }

    public function addInfo(array $data, int $userId = 0)
    {
        /** @var UsersRepository $userRepository */
        $userRepository = app()->make(UsersRepository::class);
        $userInfo = $userRepository->getInfoByMobile($data['mobile']);
        if (!$userInfo) {
            $userData['mobile'] = $data['mobile'];
            $userData['account'] = $data['mobile'];
            $userData['username'] = $data['username'];
            $userInfo = $userRepository->register($userData);
        }
        if ($userInfo['username'] != $data['username']) {
            $userRepository->update($userInfo['id'], ['username' => $data['username']]);
        }
        unset($data['mobile']);
        unset($data['username']);

        $data['user_id'] = $userInfo['id'];
        return Db::transaction(function () use ($data, $userId) {
            $res = $this->dao->create($data);
            /** @var PropertyUsersLogRepository $repository */
            $repository = app()->make(PropertyUsersLogRepository::class);
            $repository->addInfo($res['property_id'], $userId, 'ROOM.OWNER.ADD', $data);
            return $res;
        });
    }

    public function editInfo($info, array $data, int $userId = 0)
    {
        return Db::transaction(function () use ($data, $info, $userId) {
            /** @var PropertyUsersLogRepository $repository */
            $repository = app()->make(PropertyUsersLogRepository::class);
            $repository->addInfo($info['property_id'], $userId, 'ROOM.OWNER.EDIT', $data);
            return $this->dao->update($info['id'], $data);
        });
    }


    public function getDetail(int $id)
    {
        $data = $this->dao->search([])
            ->with([
                'userInfo' => function ($query) {
                    $query->field('id,username,mobile');
                    $query->bind(['username' => 'username', 'mobile' => 'mobile']);
                },
                'roomInfo' => function ($query) {
                    $query->append(['full_room_name']);
                    $query->bind(['full_room_name' => 'full_room_name']);
                }
            ])
            ->where('id', $id)
            ->find();
        return $data;
    }


    public function auditInfo($info, array $data, int $userId = 0)
    {
        return Db::transaction(function () use ($data, $info, $userId) {
            unset($data['id']);
            $data['review_time'] = date('Y-m-d H:i:s');
            $res = $this->dao->update($info['id'], $data);

            /** @var PropertyUsersLogRepository $repository */
            $repository = app()->make(PropertyUsersLogRepository::class);
            $repository->addInfo($info['property_id'], $userId, 'ROOM.OWNER.AUDIT', $data);

            return $res;
        });
    }


    public function batchDelete(array $ids, int $userId = 0)
    {
        $list = $this->dao->selectWhere([
            ['id', 'in', $ids]
        ]);
        if ($list) {
            foreach ($list as $k => $v) {
                $this->deleteInfo($v, $userId);
            }
            return $list;
        }
        return [];
    }

    public function deleteInfo($info, int $userId = 0)
    {
        /** @var PropertyUsersLogRepository $repository */
        $repository = app()->make(PropertyUsersLogRepository::class);
        $repository->addInfo($info['property_id'], $userId, 'ROOM.OWNER.DEL', $info);
        $this->dao->delete($info['id']);
    }

}